package com.tan.androidtest.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tan.androidtest.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 常用的RecycleView的adapter
 */

public class NormalRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<String> mBeans = new ArrayList<>();
    private Context mContext;

    public NormalRecycleAdapter(Context context) {
        mContext = context;
    }

    public void loadBeans(List<String> beans) {
        mBeans = beans;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_normal_list_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder viewHolder = (MyViewHolder) holder;
        String bean = mBeans.get(position);
        if (!TextUtils.isEmpty(bean)) {
            viewHolder.tv_test.setText(bean);
        }
    }

    @Override
    public int getItemCount() {
        return mBeans != null ? mBeans.size() : 0;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_test;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_test = itemView.findViewById(R.id.tv_test);
        }
    }


}
